Kotlin

Map Specific Operations

Swift

Retrieving keys and values

                  val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
println(numbersMap.get("one"))
println(numbersMap["one"])
println(numbersMap.getOrDefault("four", 10))
println(numbersMap["five"])               // null
//numbersMap.getValue("six")      // exception!
                
                    let numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
print(numbersMap.get("one"))
print(numbersMap["one"])
print(numbersMap.getOrDefault("four", 10))
print(numbersMap["five"])               // null
//numbersMap.getValue("six")      // exception!
                  
                  val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
println(numbersMap.keys)
println(numbersMap.values)
                
                    let numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
print(numbersMap.keys)
print(numbersMap.values)
                  

Filtering

                  val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
val filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10}
println(filteredMap)
                
                    let numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
let filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10}
print(filteredMap)
                  
                  val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
val filteredKeysMap = numbersMap.filterKeys { it.endsWith("1") }
val filteredValuesMap = numbersMap.filterValues { it < 10 }
​
println(filteredKeysMap)
println(filteredValuesMap)
                
                    let numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
let filteredKeysMap = numbersMap.filterKeys { it.endsWith("1") }
let filteredValuesMap = numbersMap.filterValues { $0 < 10 }
​
print(filteredKeysMap)
print(filteredValuesMap)
                  

plus and minus operators

                  val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
println(numbersMap + Pair("four", 4))
println(numbersMap + Pair("one", 10))
println(numbersMap + mapOf("five" to 5, "one" to 11))
                
                    let numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
print(numbersMap + Pair("four", 4))
print(numbersMap + Pair("one", 10))
print(numbersMap + mapOf("five" to 5, "one" to 11))
                  
                  val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
println(numbersMap - "one")
println(numbersMap - listOf("two", "four"))
                
                    let numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
print(numbersMap - "one")
print(numbersMap - listOf("two", "four"))
                  

Adding and updating entries

                  val numbersMap = mutableMapOf("one" to 1, "two" to 2)
numbersMap.put("three", 3)
println(numbersMap)
                
                    let numbersMap = mutableMapOf("one" to 1, "two" to 2)
numbersMap.put("three", 3)
print(numbersMap)
                  
                  val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMap.putAll(setOf("four" to 4, "five" to 5))
println(numbersMap)
                
                    let numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMap.putAll(setOf("four" to 4, "five" to 5))
print(numbersMap)
                  
                  val numbersMap = mutableMapOf("one" to 1, "two" to 2)
val previousValue = numbersMap.put("one", 11)
println("value associated with 'one', before: $previousValue, after: ${numbersMap["one"]}")
println(numbersMap)
                
                    let numbersMap = mutableMapOf("one" to 1, "two" to 2)
let previousValue = numbersMap.put("one", 11)
print("value associated with 'one', before: $previousValue, after: ${numbersMap["one"]}")
print(numbersMap)
                  
                  val numbersMap = mutableMapOf("one" to 1, "two" to 2)
numbersMap["three"] = 3     // calls numbersMap.set("three", 3)
numbersMap += mapOf("four" to 4, "five" to 5)
println(numbersMap)
                
                    let numbersMap = mutableMapOf("one" to 1, "two" to 2)
numbersMap["three"] = 3     // calls numbersMap.set("three", 3)
numbersMap += mapOf("four" to 4, "five" to 5)
print(numbersMap)
                  

Removing entries

                  val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMap.remove("one")
println(numbersMap)
numbersMap.remove("three", 4)            //doesn't remove anything
println(numbersMap)
                
                    let numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMap.remove("one")
print(numbersMap)
numbersMap.remove("three", 4)            //doesn't remove anything
print(numbersMap)
                  
                  val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "threeAgain" to 3)
numbersMap.keys.remove("one")
println(numbersMap)
numbersMap.values.remove(3)
println(numbersMap)
                
                    let numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "threeAgain" to 3)
numbersMap.keys.remove("one")
print(numbersMap)
numbersMap.values.remove(3)
print(numbersMap)
                  
                  val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMap -= "two"
println(numbersMap)
numbersMap -= "five"             //doesn't remove anything
println(numbersMap)
                
                    let numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMap -= "two"
print(numbersMap)
numbersMap -= "five"             //doesn't remove anything
print(numbersMap)